package com.easpeed.comon.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.easpeed.elevator.R;


/**
 *
 */
public class BasePopwindow implements BasePopwindowInterface {

    protected LayoutInflater layoutInflater;
    protected Context mContext;
    protected WindowUI ui;

    protected int screenHeight, screenWidth;


    protected BasePopwindow(Context mContext) {
        this.mContext = mContext;
        ui = new WindowUI();
        layoutInflater = LayoutInflater.from(mContext);
        ui.popupWindow = new PopupWindow(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ui.popupWindow.setBackgroundDrawable(new BitmapDrawable());
//        ui.popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        ui.popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBg(1.0f);
            }
        });

        ui.popupWindow.setOutsideTouchable(true);
        ui.popupWindow.setFocusable(true);
        ui.popupWindow.setTouchable(true);
        screenHeight = ScreenUtils.getScreenHeight();
        screenWidth = ScreenUtils.getScreenWidth();
    }

    @Override
    public void setOutsideTouchable(boolean isOutsideTouchable) {
        ui.popupWindow.setOutsideTouchable(isOutsideTouchable);
    }

    @Override
    public void setFocusable(boolean focusable) {
        ui.popupWindow.setFocusable(focusable);
    }

    @Override
    public void setTouchable(boolean touchable) {
        ui.popupWindow.setTouchable(touchable);
    }

    public class WindowUI {

        public View rootView;
        public ListView mListView;
        public GridView gridView;
        public PopupWindow popupWindow;
        public EditText input_1;
        public TextView title, content, sure, cancel, install, reset, close,text1 , text2 , text3;
        public View layout;
        public ProgressBar progressBar;

        public ImageView image;
        public RecyclerView recyclerView;
        public RadioGroup rg;


        public TextView choose, takePhoto;


        public ProgressBar getProgressBar() {
            return progressBar;
        }

        public TextView getInstall() {
            return install;
        }

        public TextView getTitle() {
            return title;
        }

        public TextView getContent() {
            return content;
        }

        public TextView getSure() {
            return sure;
        }

        public TextView getCancel() {
            return cancel;
        }

        public View getLayout() {
            return layout;
        }

        public EditText getInput_1() {
            return input_1;
        }

        public ListView getListView() {
            return mListView;
        }

        public PopupWindow getPopupWindow() {
            return popupWindow;
        }

        public GridView getGridView() {
            return gridView;
        }

        public void setGridView(GridView gridView) {
            this.gridView = gridView;
        }
    }


    /**
     * -1代表不改变
     *
     * @param height
     * @param width
     */
    @Override
    public void setHeightWidth(int height, int width) {
        if (height != -1) {
            ui.getPopupWindow().setHeight(height);
        }
        if (width != -1) {
            ui.getPopupWindow().setWidth(width);
        }
    }

    @Override
    public void updateheight(int width, int height) {
        ui.getPopupWindow().update(width, height);
    }


    @Override
    public void setContentView(View v) {
        ui.rootView = v;
        ui.getPopupWindow().setContentView(v);
    }

    @Override
    public void dismiss() {
        if (ui != null && ui.popupWindow != null && ui.popupWindow.isShowing()) {
            ui.popupWindow.dismiss();
        }
    }

    @Override
    public void setBg(float f) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = f;
        if (f != 1.0f) {
            ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            ((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    @Override
    public void setAnimationStyle(int style) {
        ui.popupWindow.setAnimationStyle(style);
    }


    /**
     * 默认动画
     */
    public void setAnimationStyle() {
//        ui.popupWindow.setAnimationStyle(R.style.popwin_anim_style);
    }


}
